/*pmFWCFu5nhKkBzYmrkBakSV6B6xE5zJS9Xlf9U/PYPi9wdgsReU89OhlkJs/0fgI*/
/*
 * @Author: xiahui
 * @PageInfo: 采购合同维护列表态模板
 * @Date: 2019-01-10 17:03:20
 * @Last Modified by: zhangmi
 * @Last Modified time: 2021-09-10 12:08:25
 */
import {
	PAGECODE,
	AREA,
	URL,
	DATASOURCECACHE,
	DEFCACHEKEY,
	FIELDS,
	UISTATUS,
	FSTATUSFLAG,
	VERSION,
	BUTTONID
} from '../../constance';
import { buttonClick } from '../btnClicks';
import { setRefMainOrgPermissonByItem } from '../../../pub/utils/referUtil';
import { setPsndocShowLeavePower, setRefShowDisabledData } from '../../../../../scmpub/scmpub/pub/tool/refUtils';
import { buttonControl } from '../viewControl/buttonControl';
import transtypeUtils from '../../../../../scmpub/scmpub/pub/tool/transtypeUtils';
import { getListDisableHotKeyBtn } from '../../../../../scmpub/scmpub/pub/tool/hotKeysUtil';
import { getDefData } from '../../../../../scmpub/scmpub/pub/cache';
import { getLangByResId } from '../../../../../scmpub/scmpub/pub/tool/multiLangUtil';

const { init, initQuery } = transtypeUtils;

export default function(props) {
	props.createUIDom(
		{
			pagecode: PAGECODE.listPagecode
		},
		(data) => {
			if (data) {
				if (data.context) {
					init.call(this, data.context); // 缓存交易类型
				}
				if (data.button) {
					let button = data.button;
					props.button.setButtons(button);
					props.button.setPopContent(
						BUTTONID.Delete,
						getLangByResId(this, '4004PURDAILY-000059')
					); /* 国际化处理： 确定要删除吗？*/
					// buttonControl(props); // 初始化按钮状态
					// 刷新按钮
					let cacheData = getDefData(DATASOURCECACHE.dataSourceListCacheKey, DEFCACHEKEY.queryCacheKey);
					props.button.setDisabled(BUTTONID.Refresh, cacheData ? false : true);
				}
				if (data.template) {
					let meta = data.template;
					modifierMeta.call(this, props, meta);
					props.meta.setMeta(meta, () => {
						buttonControl.call(this, props);
					});
				}
			}
		}
	);
}

function modifierMeta(props, meta) {
	//修改列渲染样式
	meta[AREA.listTableId].items = meta[AREA.listTableId].items.map((item, key) => {
		if (item.attrcode == FIELDS.vbillcode) {
			item.render = (text, record, index) => {
				if (record && record.vbillcode) {
					return (
						<a
							style={{ cursor: 'pointer' }}
							onClick={() => {
								props.pushTo(URL.card, {
									status: UISTATUS.browse,
									id: record.pk_ct_pu.value,
									pagecode: PAGECODE.cardPagecode
								});
							}}
						>
							{record.vbillcode.value}
						</a>
					);
				}
			};
		}
		return item;
	});

	//添加操作列
	meta[AREA.listTableId].items.push({
		label: getLangByResId(this, '4004PURDAILY-000050') /* 国际化处理： 操作*/,
		itemtype: 'customer',
		attrcode: 'opr',
		width: '200px',
		visible: true,
		fixed: 'right',
		render: (text, record, index) => {
			let fstatusflag = record[FIELDS.fstatusflag].value;
			let version = parseInt(record[FIELDS.version].value);

			let buttonAry = null;
			switch (fstatusflag) {
				case FSTATUSFLAG.free:
					if (VERSION.v1 == version) {
						buttonAry = [
							BUTTONID.Commit,
							BUTTONID.Edit,
							BUTTONID.Delete,
							BUTTONID.Copy,
							BUTTONID.ApproveInfo
						];
					} else {
						buttonAry = [
							BUTTONID.Commit,
							BUTTONID.Modify,
							BUTTONID.ModifyDelete,
							BUTTONID.Copy,
							BUTTONID.ApproveInfo
						];
					}
					break;
				case FSTATUSFLAG.approving:
					buttonAry = [ BUTTONID.Copy, BUTTONID.ApproveInfo ];
					break;
				case FSTATUSFLAG.approve:
					buttonAry = [ BUTTONID.Validate, BUTTONID.Copy, BUTTONID.ApproveInfo ];
					break;
				case FSTATUSFLAG.unapprove:
					if (VERSION.v1 == version) {
						buttonAry = [ BUTTONID.Edit, BUTTONID.Copy, BUTTONID.ApproveInfo ];
					} else {
						buttonAry = [ BUTTONID.Modify, BUTTONID.Copy, BUTTONID.ApproveInfo ];
					}
					break;
				case FSTATUSFLAG.validate:
					buttonAry = [ BUTTONID.Modify, BUTTONID.Copy, BUTTONID.ApproveInfo ];
					break;
				case FSTATUSFLAG.frozen:
					buttonAry = [ BUTTONID.UnFreeze, BUTTONID.Copy, BUTTONID.ApproveInfo ];
					break;
				case FSTATUSFLAG.terminate:
					buttonAry = [ BUTTONID.UnEnd, BUTTONID.Copy, BUTTONID.ApproveInfo ];
					break;
			}
			return props.button.createErrorButton({
				record: record,
				showBack: false, // 是否显示回退按钮
				sucessCallBack: () => {
					return props.button.createOprationButton(buttonAry, {
						area: AREA.list_inner,
						ignoreHotkeyCode: getListDisableHotKeyBtn(),
						buttonLimit: 3,
						onButtonClick: (props, key) => buttonClick.call(this, props, key, text, record, index)
					});
				}
			});
		}
	});

	searchRefFilter(props, meta, AREA.searchId);
	// 发布交易类型后，需要控制查询模板交易类型的值和可编辑性
	initQuery.call(this, props, meta, AREA.searchId, FIELDS.ctrantypeid);
	return meta;
}

// 查询区参照过滤
function searchRefFilter(props, meta, moduleId) {
	meta[moduleId].items.map((item) => {
		// 显示离职人员
		setPsndocShowLeavePower(item);
		// 显示停用
		setRefShowDisabledData(item);

		//主组织过滤
		if (item.attrcode === FIELDS.pk_org) {
			setRefMainOrgPermissonByItem(item);
		} else if (item.attrcode == FIELDS.ctrantypeid) {
			// 交易类型
			item.queryCondition = () => {
				return {
					GridRefActionExt: 'nccloud.web.ct.purdaily.ref.Z2BillTypeRefSql'
				};
			};
		} else if (item.attrcode === FIELDS.cvendorid) {
			item.isShowUnit = true;
			// 供应商
			item.queryCondition = () => {
				let pk_org = (props.search.getSearchValByField(moduleId, FIELDS.pk_org) || {}).value;
				return {
					pk_org: (pk_org || {}).firstvalue
				};
			};
		} else if (item.attrcode === FIELDS.depid) {
			// 采购部门
			item.queryCondition = () => {
				let pk_org = (props.search.getSearchValByField(moduleId, FIELDS.pk_org) || {}).value;
				return {
					pk_org: (pk_org || {}).firstvalue,
					busifuncode: 'pu'
				};
			};
		} else if (item.attrcode === FIELDS.personnelid) {
			// 采购员
			item.isShowUnit = false;
			item.queryCondition = () => {
				let pk_org = (props.search.getSearchValByField(moduleId, FIELDS.pk_org) || {}).value;
				let pk_dept = (props.search.getSearchValByField(moduleId, FIELDS.depid) || {}).value;
				return {
					pk_org: (pk_org || {}).firstvalue,
					pk_dept: (pk_dept || {}).firstvalue,
					busifuncode: 'pu'
				};
			};
			//
		} else if (item.attrcode === 'pk_ct_pu_b.pk_srcmaterial' || item.attrcode === 'pk_ct_pu_b.casscustid') {
			item.isShowUnit = true;
			// 物料
			item.queryCondition = () => {
				let pk_org = (props.search.getSearchValByField(moduleId, FIELDS.pk_org) || {}).value;
				return {
					pk_org: (pk_org || {}).firstvalue
					// SCM_DISCOUNTFLAG: 'N',
					// SCM_FEEFLAG: 'N',
					// GridRefActionExt: 'nccloud.web.scmpub.ref.MaterialRefFilterUtils',
				};
			};
		} else if (item.attrcode.match('vbdef') || item.attrcode.match('vfree') || item.attrcode.match('vdef')) {
			//自定义项，自由辅助属性
			item.queryCondition = () => {
				let pk_org = (props.search.getSearchValByField(moduleId, FIELDS.pk_org) || {}).value;
				// 财务组织单选时，进行过滤
				if (pk_org && pk_org.firstvalue.indexOf(',') == -1) {
					return { pk_org: pk_org.firstvalue };
				} else {
					return {};
				}
			};
		} else {
			item.queryCondition = () => {
				let pk_org = (props.search.getSearchValByField(moduleId, FIELDS.pk_org) || {}).value;
				return {
					pk_org: (pk_org || {}).firstvalue
				};
			};
		}
	});
}

/*pmFWCFu5nhKkBzYmrkBakSV6B6xE5zJS9Xlf9U/PYPi9wdgsReU89OhlkJs/0fgI*/